/**
 * 直接更新 state的多个方法的对象
 */
import {
  RECEIVE_USER_INFO,
  RESET_USER_INFO,
  RECEIVE_MENU_LIST,
  ERROR_MSG,
  RECEIVE_USER_PAGE,
  UPDATE_USER_STATE,
  SUCCESS_MSG
} from '../store/mutation-types'

import { Message } from 'element-ui'

export default {
  [RECEIVE_USER_INFO] (state, { userInfo }) {
    state.userInfo = userInfo
  },
  [RESET_USER_INFO] (state) {
    // 清除 sessionStorage 中的token
    window.sessionStorage.clear()
    // 将 state 中的userInfo置空
    state.userInfo = {}
    Message.success('退出成功')
  },
  [RECEIVE_MENU_LIST] (state, { menuList }) {
    state.menuList = menuList
  },
  [ERROR_MSG] (state, { msg }) {
    // 显示错误信息
    Message.success(msg)
  },
  [SUCCESS_MSG] (state, { msg }) {
    // 显示成功信息
    Message.success(msg)
  },
  [RECEIVE_USER_PAGE] (state, data) {
    state.userPage = data
  },
  [UPDATE_USER_STATE] (state, data) {
    // 遍历分页对象中的users，找出与data中user的id相同的用户，并把状态修改
    if (data) {
      state.userPage.users.forEach(user => {
        if (user.id === data.id) {
          user.mg_state = !!data.mg_state
        }
      })
    }
  }
}
